package com.jisu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jisu.common.ResultVo;
import com.jisu.mapper.CarPartMapper;
import com.jisu.pojo.CarPart;
import com.jisu.service.CarPartService;
import io.micrometer.common.util.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarPartServiceImpl extends ServiceImpl<CarPartMapper, CarPart> implements CarPartService {

    private final CarPartMapper carPartMapper;

    public CarPartServiceImpl(CarPartMapper carPartMapper) {
        this.carPartMapper = carPartMapper;
    }

    //添加车辆部件
    @Override
    public ResultVo<CarPart> addCarPart(CarPart carPart) {
        if (carPart == null){
            return ResultVo.fail("参数不能为空");
        }

        int rows = carPartMapper.insert(carPart);
        if (rows > 0){
            return ResultVo.success("添加成功", carPart);
        }else {
            return ResultVo.fail("添加失败");
        }
    }

    //删除车辆部件
    @Override
    public ResultVo deleteCarPart(List<Integer> ids) {
        if (ids == null || ids.isEmpty()){
            return ResultVo.fail("ID列表不能为空");
        }
        int rows = carPartMapper.deleteByIds(ids);
        if (rows > 0){
            return ResultVo.success("删除成功,删除行数为：", rows);
        }
        return ResultVo.fail("删除失败");
    }

    //更新车辆部件
    @Override
    public ResultVo<CarPart> updateCarPart(CarPart carPart) {
        if(carPart.getCarPartId() ==  null){
            return ResultVo.fail("更新失败，ID不能为空");
        }
        int rows = carPartMapper.updateById(carPart);
        if (rows > 0){
            return ResultVo.success("更新成功", carPart);
        }
        return ResultVo.fail("更新失败,请检查更新信息");
    }


    //查询车辆部件,多条件查询
    @Override
    public ResultVo<List<CarPart>> getCarPart(CarPart carPart) {
        LambdaQueryWrapper<CarPart> queryWrapper = new LambdaQueryWrapper<>();
        if(carPart != null){
            queryWrapper.eq(carPart.getCarPartId() != null, CarPart::getCarPartId, carPart.getCarPartId());
            queryWrapper.like(StringUtils.isNotBlank(carPart.getPartName()),CarPart::getPartName,carPart.getPartName());
            queryWrapper.eq(carPart.getModelId() != null,CarPart::getModelId,carPart.getModelId());
        }
        List<CarPart> list = carPartMapper.selectList(queryWrapper);
        return ResultVo.success("查询成功", list);
    }
}
